package org.smallreunion.controller.bbs;

import org.slim3.controller.Navigation;
import org.slim3.util.StringUtil;
import org.smallreunion.controller.AbstractController;
import org.smallreunion.service.FeedService;

import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.SyndFeedOutput;

/**
 * BBSFeedController
 *
 * BBSのRSSを生成するコントローラ
 *
 * @author Sinmetal
 *
 */
public class FeedController extends AbstractController {

    private static final String MIME_TYPE = "application/xml; charset=UTF-8";

    /** FeedService */
    private FeedService feedService = new FeedService();

    /**
     * feedTypeに応じたRSSを表示
     */
    @Override
    public Navigation run() throws Exception {
        String feedType = getFeedType(asString("feedType"));

        SyndFeed feed = feedService.getFeed();
        feed.setFeedType(feedType);
        response.setContentType(MIME_TYPE);
        SyndFeedOutput output = new SyndFeedOutput();
        output.output(feed, response.getWriter());

        return null;
    }

    /**
     * 引数のFeedTypeから、ROMEのFeedTypeを取得する
     *
     * @param reqFeedType
     * @return
     */
    protected String getFeedType(String reqFeedType) {
        final String RSS10 = "rss_1.0";
        final String RSS20 = "rss_2.0";

        if (StringUtil.isEmpty(reqFeedType)) {
            return RSS10;
        }

        final String RSS10_PARAM = "10";
        final String RSS20_PARAM = "20";

        if (RSS10_PARAM.equals(reqFeedType)) {
            return RSS10;
        } else if (RSS20_PARAM.equals(reqFeedType)) {
            return RSS20;
        }

        return RSS10;
    }
}
